﻿using System.Collections.Generic;
using System.Web;

namespace OpenWaves.Pagination
{
    public class PaginationController
    {
        private readonly ICurrentPageIndexProvider currentPageIndexProvider;

        public PaginationController(HttpRequest request)
            : this(new PaginationUrlFactory(request))
        {
        }

        public PaginationController(ICurrentPageIndexProvider currentPageIndexProvider)
        {
            this.currentPageIndexProvider = currentPageIndexProvider;

            this.ItemsPerPage = 10;
            this.MaxNumberOfPagesToShow = 5;
        }

        public PaginationViewModel<T> Paginate<T>(IEnumerable<T> items)
        {
            int totalItemsCount;
            var pages = items.Paginate(this.ItemsPerPage, out totalItemsCount);

            return PaginationViewModel.Create(
                totalItemsCount, 
                pages,
                this.currentPageIndexProvider.CurrentPageIndex,
                this.MaxNumberOfPagesToShow);
        }

        public int ItemsPerPage { get; set; }

        public int MaxNumberOfPagesToShow { get; set; }
    }
}